function buildAjaxObject(){
	var ajaxRequest;  // Ajax object;
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Browser do not support Ajax
				alert("Your browser doesn't support Ajax!");
				return false;
			}
		}
	}
	
	return ajaxRequest;
}

function checkEmail(email){
	
	var ajaxObj=buildAjaxObject();
	elem=document.getElementById("emailAvailablilityCheck");
	if(email!=""){
		ajaxObj.onreadystatechange=function(){
			if(ajaxObj.readyState==0){
			}
			if(ajaxObj.readyState==2){
				elem.getElementsByTagName('span')[0].id="waiting";
				elem.getElementsByTagName('span')[0].innerHTML="Please wait!";			
			}		
			if(ajaxObj.readyState==4){ // Get the data from the server's response
				var temp=ajaxObj.responseText;
				if(temp.match("false")!=null){ // find whether there's false in the return string
					elem.getElementsByTagName('span')[0].id="available";
					elem.getElementsByTagName('span')[0].innerHTML="Congraduation! This Email is available";
				}else{
					elem.getElementsByTagName('span')[0].id="unavailable";
					elem.getElementsByTagName('span')[0].innerHTML="Sorry, the Email has been taken up.";
				}				
			}
		}
		ajaxObj.open("GET", "checkEmail.do?useremail="+email, true);
		ajaxObj.send(null);	
	}else{
		elem.getElementsByTagName('span')[0].id="";
		elem.getElementsByTagName('span')[0].innerHTML="";				
	}
}

var commentDiv;
var commentBody;
var curElement;

function displayComment(id, element){

    
	var ajaxObj=buildAjaxObject();
	commentBody = document.getElementById("commentBody");
	commentDiv = document.getElementById("popup");
	curElement=element;
	ajaxObj.onreadystatechange=function(){
		if(ajaxObj.readyState==4){ // Get the data from the server's response
			if(ajaxObj.status==200){
				setdata(ajaxObj.responseXML);
			}
		}
	}
	ajaxObj.open("GET", "displayTag.do?commentId="+id, true);
	ajaxObj.send(null);	
	
}

function setdata(data) {           
	cleardata();
	setoffsets();
	var content=data.getElementsByTagName("comment")[0].childNodes[0].nodeValue;
	var row=createrow(content);
}

function createrow(data) {           
	var row, cell, txtnode;
	var insertPos=commentBody.getElementsByTagName("tr").length;
	row = commentBody.insertRow(insertPos);
	cell = row.insertCell(0);
	cell.setAttribute("bgcolor", "#fffafa");
	cell.setAttribute("border", "0");                          
	cell.innerHTML=data;
	return row; 
}

function setoffsets() {
	var width=curElement.offsetWidth;
	var top = calculateOffsetTop(curElement);
	var left = calculateOffsetLeft(curElement);
	commentDiv.style.border = "black 1px solid";
	commentDiv.style.left = left + width + "px";
	commentDiv.style.top = top + "px";
}


function calculateOffsetTop(field) {
	return calculateOffset(field, "offsetTop");
}

function calculateOffsetLeft(field) {
	return calculateOffset(field, "offsetLeft");
}

function calculateOffset(field, attr) {
	var offset = 0;
	while(field) {
		offset += field[attr];
		field = field.offsetParent;
	}
	return offset;
}

function cleardata() {           
	var ind = commentBody.getElementsByTagName("tr").length;           
	for (var i = ind - 1; i >= 0 ; i--) {
		commentBody.deleteRow(i);      
	}
	commentDiv.style.border = "none";
}


